/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
public class DeThi {

    //Danh sach cac thuoc tinh cua de thi
    private int maDe;
    private String tieuDe;
    private String userName;

    //Danh sach ham get va set
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public int getMaDe() {
        return maDe;
    }

    public void setMaDe(int maDe) {
        this.maDe = maDe;
    }

    public String getTieuDe() {
        return tieuDe;
    }

    public void setTieuDe(String tieuDe) {
        this.tieuDe = tieuDe;
    }

    //Danh sach ham khoi tao de thi
    public DeThi(int maDe, String tieuDe) {
        this.maDe = maDe;
        this.tieuDe = tieuDe;
    }

    
    public DeThi() {
    }

    //Ham lay danh sach de thi
    public static List<DeThi> layDanhSachDeThi() {
        List<DeThi> danhSach = new ArrayList<DeThi>();
        String sql = "select * from de_thi";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                DeThi dt = new DeThi();
                dt.maDe = rs.getInt("id");
                dt.tieuDe = rs.getString("tieu_de");
                danhSach.add(dt);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return danhSach;
    }
  //Ham lay danh sach de thi theo nguoi dung
    public static List<DeThi> layDanhSachDeThiTheoNguoiDung(String username) {
        List<DeThi> danhSach = new ArrayList<DeThi>();
        String sql = "select * from de_thi where user_name='"+username+"'";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                DeThi dt = new DeThi();
                dt.maDe = rs.getInt("id");
                dt.tieuDe = rs.getString("tieu_de");
                danhSach.add(dt);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return danhSach;
    }

    //Ham lay de thi theo id
    public static DeThi layDeThiTheoID(int id) {
        DeThi dt = null;
        String sql = "select * from de_thi where id=" + id;
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                dt = new DeThi();
                dt.maDe = rs.getInt("id");
                dt.tieuDe = rs.getString("tieu_de");
                return dt;

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return dt;
    }
    //Lay danh sachde thi theo username
    public static List<DeThi>layDSDeThiTheoUsername(String user_name){
        List<DeThi>dsDeThi=null;
        String sql = "select de_thi.id,de_thi.tieu_de from de_thi"
                + " inner join bai_thi_cua_nguoi_dung on ma_de_thi=de_thi.id"
                + " where user_name='"+user_name+"'";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                DeThi dt = new DeThi();
                dt.maDe = rs.getInt("id");
                dt.tieuDe = rs.getString("tieu_de");
                dsDeThi.add(dt);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return dsDeThi;
    }

    //Ham them de thi
    public boolean ThemDeThi() {
        boolean rs = false;
        String sql = "insert into de_thi(tieu_de,user_name)values('"+this.tieuDe+"','"+this.userName+"')";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu

            if (statement.executeUpdate() == 1) {
                rs = true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }

    //Ham cap nhat nguoi dung
    public boolean CapNhatDeThi() {
        boolean rs = false;
        String sql = "update de_thi set tieu_de='"+this.tieuDe+"' where id="+this.maDe;
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu

            if (statement.executeUpdate() == 1) {
                rs = true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }

    //Ham xoa de thi
    public static boolean XoaDeThi(int id) {
        boolean rs = false;
        String sql = "delete from de_thi where id="+id;
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu

            if (statement.executeUpdate() == 1) {
                rs = true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }
}
